package com.mali.malibackendproduct;

import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.mali.backendcommon.rpc.UserService;
import com.mali.backendcommon.config.WebMvcConfiguration;
import com.mali.malibackendproduct.service.impl.ProductServiceImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

@MapperScan(basePackages = "com.mali.malibackendproduct.mapper")
@SpringBootApplication
@ComponentScan("com.mali")
@Import(WebMvcConfiguration.class)
public class MaliBackendProductApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(MaliBackendProductApplication.class, args);
    }
    @Bean
    public UserService userService() {
        ConsumerConfig<UserService> consumerConfig = new ConsumerConfig<UserService>()
                .setInterfaceId(UserService.class.getName())
                .setProtocol("bolt")
                .setDirectUrl("bolt://127.0.0.1:12200") // 服务 B 的地址
                .setUniqueId("1.0");                    // 设置版本号，与服务 B 一致
        return consumerConfig.refer();
    }
    @Bean
    public ProductService productService() {
        return new ProductServiceImpl();
    }
    @Override
    public void run(String... args) throws Exception {
        // 设置服务器配置
        ServerConfig serverConfig = new ServerConfig()
                .setProtocol("bolt") // 设置协议
                .setPort(12201)       // 设置端口
                .setDaemon(false);

        // 发布服务
        ProviderConfig< ProductService> providerConfig = new ProviderConfig< ProductService>()
                .setInterfaceId( ProductService.class.getName()) // 设置接口
                .setRef(productService())                        // 设置实现
                .setServer(serverConfig)
                .setUniqueId("1.0");                           // 设置版本号

        providerConfig.export(); // 发布服务
    }

}
